package Game;

import static org.junit.Assert.assertTrue;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import Map.FactoryTuile;
import Map.Tuile;
import Unite.AttributeMap;
import Unite.FactoryUnit;
import Unite.Legion;
import Unite.Unit;

public class CombatTest {
   Combat _combat;
   Legion _attackingLegion;
   Legion _defendingLegion;
   private Unit _archer;
   private Unit _chevalier;
   private TableValeurs _valTable;
   
   @Before
   public void setUp() {
	   _valTable = new TableValeurs();
      _archer = FactoryUnit.CreateUnit(Unit.ARCHER, new AttributeMap(5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5), 0);
      _chevalier = FactoryUnit.CreateUnit(Unit.CHEVALIER, new AttributeMap(5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5), 0);
      FactoryUnit.CreateUnit(Unit.TORUK, new AttributeMap(5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5), 0);
      FactoryUnit.CreateUnit(Unit.GUERRIER, new AttributeMap(5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5), 0);
      _attackingLegion = new Legion(null);
      _defendingLegion = new Legion(null);
      _combat = new Combat();
   }
   
   @After
   public void tearDown() {
	   _valTable = null;
	   _archer = null;
	   _chevalier = null;
	   _attackingLegion = null;
	   _defendingLegion = null;
	   _combat = null;
   }

   @Test
   public void testCalculateRatio() {
      PairingTable pairingTable;
      
      _attackingLegion.add(_archer.getCopy());
      _attackingLegion.add(_archer.getCopy());
      _defendingLegion.add(_archer.getCopy());
      _defendingLegion.add(_archer.getCopy());
      
      pairingTable = Combat.calculateRatio(_attackingLegion, _defendingLegion);
      
      for (Unit attackingUnit : _attackingLegion.getUnits())
         for (Unit defendingUnit : _defendingLegion.getUnits())
            assertTrue(pairingTable.getRatio(attackingUnit, defendingUnit) == 0.5);
      
      for (Unit attackingUnit : _defendingLegion.getUnits())
         for (Unit defendingUnit : _attackingLegion.getUnits())
            assertTrue(pairingTable.getRatio(attackingUnit, defendingUnit) == 0.5);

      _attackingLegion.add(_chevalier.getCopy());
      _attackingLegion.add(_chevalier.getCopy());
      
      pairingTable = Combat.calculateRatio(_attackingLegion, _defendingLegion);
      
      for (Unit attackingUnit : _defendingLegion.getUnits())
         for (Unit defendingUnit : _attackingLegion.getUnits())
            assertTrue(pairingTable.getRatio(attackingUnit, defendingUnit) == 0.3333333333333333
                    || pairingTable.getRatio(attackingUnit, defendingUnit) == 0.16666666666666666);
           
   }

   @Test
   public void testCalculateResults() {
      PairingTable resultsTable;
      PairingTable pairingTable;
      
      _attackingLegion.add(_archer.getCopy());
      _attackingLegion.add(_archer.getCopy());
      _defendingLegion.add(_archer.getCopy());
      _defendingLegion.add(_archer.getCopy());
      
      pairingTable = Combat.calculateRatio(_attackingLegion, _defendingLegion);
      resultsTable = Combat.calculateResults(_valTable, _attackingLegion, _defendingLegion, 
         FactoryTuile.CreateTuile(Tuile.TERRE), pairingTable);

      resultsTable.print();
   }

}
